
package com.ltg.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Gallery;

import java.util.Random;

@SuppressWarnings("deprecation")
public class TestGallery extends Gallery {
    private Paint paint;
    private Random random = new Random();
    private boolean attatch;
    private float degree = 1.2f;
    private Runnable task;
    Canvas mCanvas = new Canvas();
    Matrix mMatrix = new Matrix();

    public TestGallery(Context context) {
        super(context);
        initPaint();
    }

    public TestGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    private void initPaint() {
        setWillNotDraw(false);
        paint = new Paint();
        paint.setColor(0xFF55CCFF);
        paint.setStyle(Style.FILL);
        setDrawingCacheEnabled(false);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2, paint);
        canvas.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight(), paint);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        attatch = true;
    }

    int itemWidth = 300;

    @Override
    protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
        int count = canvas.save();
        int centerX = getWidth() / 2;
        int leftX = (int) (centerX - itemWidth * degree / 2);
        int rightX = (int) (centerX + itemWidth * degree / 2);
        mMatrix.reset();
        float scale = degree;
        if (child.getLeft() + child.getWidth() / 2 <= leftX) {
            scale = (degree - 1)
                    * (1 - Math
                            .abs((child.getLeft() + child.getWidth() / 2f - leftX))
                            / leftX) + 1;
            mMatrix.setScale(scale, scale, child.getRight(), child.getTop()
                    + child.getHeight() / 2);
        } else if (child.getLeft() + child.getWidth() / 2 >= rightX) {
            scale = (degree - 1)
                    * (1 - Math
                            .abs((child.getLeft() + child.getWidth() / 2f - rightX))
                            / leftX) + 1;
            mMatrix.setScale(scale, scale, child.getLeft(), child.getTop()
                    + child.getHeight() / 2);
        } else {
            int x = child.getRight()
                    + (child.getLeft() + child.getWidth() / 2 - centerX);
            int y = child.getTop() + child.getHeight() / 2;
            x = Math.abs(x - centerX) * 2 / child.getWidth();
            mMatrix.setScale(scale, scale, centerX, y);
        }
        canvas.concat(mMatrix);
        boolean res = super.drawChild(canvas, child, drawingTime);
        canvas.restoreToCount(count);
        return res;

    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        attatch = false;
    }

    @Override
    protected boolean getChildStaticTransformation(View child, Transformation t) {
        t.clear();
        int index = indexOfChild(child);
        int count = getChildCount();
        if (index <= count / 2) {
            t.setAlpha(0.8f * index * 2 / count + 0.1f);
        } else {
            t.setAlpha(0.8f * (index - count / 2) / count + 0.1f);
        }
        return false;
    }

    @Override
    protected int getChildDrawingOrder(int childCount, int i) {
        boolean result = true;
        // 0, count - 1, 1, count - 2,2, count - 3, 3
        // 0, 1, 2, 3,4, 5, 6
        int selectedIndex = getSelectedItemPosition()
                - getFirstVisiblePosition();
        if(result){
            return i;
        }
        // if (i % 2 == 0) {
        // return i / 2;
        // } else {
        // return childCount - (i + 1) / 2;
        // }

        // int selectedIndex = mSelectedPosition - mFirstPosition;

        // Just to be safe
        if (selectedIndex < 0)
            return i;

        if (i == childCount - 1) {
            // Draw the selected child last
            return selectedIndex;
        } else if (i >= selectedIndex) {
            // Move the children to the right of the selected child earlier one
            return childCount - 1 - (i - selectedIndex);
        } else {
            // Keep the children to the left of the selected child the same
            return i;
        }

    }

}
